<template>
	<view class="control">
		<block v-if="shop.type === 5">
			<block v-if="source.status === 0 && source.paystatus === 0">
				<navigator hover-class="none" class="box" :url="'/pages/orders/detail?batchcode='+source.batchcode+str" v-if="!type">去支付</navigator>
				<block v-else-if="type === 1">
					<view @tap="cancelTo">取消订单</view>				
					<view class="topay" @tap="$emit('onPay')">去支付</view>				
				</block>
			</block>
			<block v-if="source.status === 0 && source.paystatus === 1 && source.sendstatus === 0">
				<navigator hover-class="none" :url="'/pages/orders/refund?batchcode='+source.batchcode + str">申请售后</navigator>
				<view @tap="shopRemind" :class="{box: !type ,special: type === 1}">提醒发货</view>
			</block>
			<block v-if="source.status === 0 && source.paystatus === 1 && source.sendstatus === 1">
				<navigator hover-class="none" :url="'/pages/orders/refund?batchcode='+source.batchcode + str">申请售后</navigator>
				<navigator hover-class="none" :url="'/pages/orders/logistics?batchcode='+source.batchcode+'&postid='+source.expressnum+'&expressname='+source.send_express_name" v-if="type === 1">查看物流</navigator>
				<view @tap="conOrder" :class="{ box: !type, special: type === 1}">确认收货</view>
			</block>
			<block v-if="source.status === 0 && source.sendstatus === 2 && source.is_discuss === 0 || source.status === 1 && source.sendstatus === 2 && source.is_discuss === 0">
				<!-- <navigator hover-class="none" :url="'/pages/orders/refund?batchcode='+source.batchcode">申请售后</navigator> -->
				<navigator hover-class="none" v-if="!type" class="box" :url="'/pages/orders/appraise?batchcode='+source.batchcode + str">评价晒单</navigator>
				<navigator hover-class="none" class="special" v-if="type === 1" :url="'/pages/orders/appraise?batchcode='+source.batchcode + str">评价晒单</navigator>	
			</block>
			<block v-if="source.return_status === 0 && source.sendstatus>2">
			<!-- 申请售后中 -->
			</block>
			<block v-if="source.return_status === -1 && source.sendstatus>2">退货失败</block>
			<block v-if="source.return_status === 1 && source.sendstatus>2">退货成功</block>
			<block v-if="source.return_status === 2 && source.sendstatus>2">
			<!-- 商家同意退货 -->
			</block>
			<block v-if="source.return_status === 3 && source.sendstatus>2">驳回请求</block>
			<block v-if="(source.return_status === 4 || source.return_status === 5) && source.sendstatus>2">用户退货中</block>
			<block v-if="(source.return_status=== 6) && source.sendstatus>2">商家已收货</block>
			<block v-if="source.return_status === 8 && source.sendstatus === 5">
				<!-- 同意退款 -->
			</block>
			<block v-if="source.return_status === 8 && source.sendstatus === 6"><!-- 已退款 --></block>
			<block v-if="source.return_status === 9 && source.sendstatus > 2">驳回退款</block>
			<block v-if="source.status === -1">
				<!-- 订单已取消 -->
			</block>
			<block v-if="source.status === 0 && source.sendstatus === 2 && source.is_discuss === 1 || source.status === 1 && source.sendstatus === 2 && source.is_discuss === 1">
				<block v-if="type === 1">
					<navigator hover-class="none" class="special" :url="'/pages/orders/logistics?batchcode='+source.batchcode+'&postid='+source.expressnum+'&expressname='+source.send_express_name">查看物流</navigator>
				</block>
			</block>
			<block v-if="source.status === -2">
				<navigator v-if="!type || type === 1" hover-class="none" :class="['box',{ special: type === 1 }]" :url="'/pages/orders/productinfo?pid='+source.pid">再次购买</navigator>
			</block>			
		</block>
		<block v-if="shop.type === 1">
			<block v-if="source.status === 0 && source.pay_status === 0">
				<block v-if="type === 1">
					<view class="box special" @tap="$emit('onPay')">去付款</view>
				</block>
				<block v-else><view class="box" @tap="toDetail">去付款</view></block>
			</block>
			<block v-if="source.status === 1 || source.status === 9 || source.status === 11 || source.status === 13">
				<view :class="['box', { special: type === 1 }]" @tap="shopLine">提醒发货</view>
			</block>
			<block v-if="source.status === 10">
				<block v-if="type === 1">实</block>
				<block v-else>待收获</block>				
			</block>
			<block v-if="source.status === 4 || source.status === 3">
				<block v-if="type === 1">
					<view class="box special">立即评价</view>
				</block>
				<block v-else><view class="box">立即评价</view></block>	
			</block>	
			<block v-if="source.status === -1">
				<block v-if="type === 1">应</block>
				<block v-else>已取消</block>				
			</block>						
		</block>		
	</view>
</template>

<script>
	import { orderRemind, confirmOrder, CancelOrder, BagConfirm, BagConcel } from '@/common/api/users'
	import { shopOrders } from '@/common/api/shop'
	import { mapGetters } from 'vuex'
	export default {
		props: {
			source: Object,
			type: {
				type: Number,
				default: 0
			}
		},
		computed: {
			...mapGetters(['shop']),
			str () {
				return this.source.tag ? `&tag=${this.source.tag}` : ''
			}
		},
		methods: {
			toDetail () {
				uni.navigateTo({
					url: '/pages/orders/detail',
					success: () => {
						uni.setStorageSync('onLineDetails', this.source)
					}
				})
			},
			shopLine () { // 线下商城提醒发货
			   uni.showLoading({ mask: true })
			   shopOrders({ batchcode: this.source.batchcode, op: 'remind' }, this.shop.customer_id).then(res => {
				   uni.hideLoading()
				   if (res.result === 1) {
					   uni.showToast({
							title: '提醒成功'
					   })					   
				   } else {
					   uni.showToast({
							title: res.msg,
							icon: 'none'
					   })					   
				   }
			   })
			},
			shopRemind () { // 提醒发货
				uni.showLoading({ mask: true })
				orderRemind({ batchcode: this.source.batchcode }).then(res => {
					uni.hideLoading()
					if (res.code === 400000) {
						uni.showModal({
							content: res.message,
							showCancel: false
						})
					}
				})
			},
			conOrder () { // 确认收货
			    uni.showModal({
			    	content: '您确认收货吗？',
					success: rst => {
						if (rst.confirm) {
							uni.showLoading({ mask: false })
							let submitOrder = +this.source.tag === 6 ? BagConfirm : confirmOrder  
							submitOrder({ batchcode: this.source.batchcode }).then(res => {
								uni.hideLoading()
								if (res.code === 400000 || res.status) {
									uni.showModal({
										content: res.message,
										showCancel: false,
										success: cm => {
											if (cm.confirm) {
												if (!this.type) {
													this.source.sendstatus = 2
												} else {
													uni.redirectTo({
														url: '/pages/orders/list'
													})
												}
											}
										}
									})
								} else {
									uni.showModal({
										content: res.message,
										showCancel: false
									})									
								}
							})
						}
					}
			    })
			},
			cancelTo () { // 取消订单
			    uni.showModal({
			    	content: '你确认取消订单吗？',
					success: rst => {
						if (rst.confirm) {
							uni.showLoading({ mask: false })
							let cancel = +this.source.tag === 6 ? BagConcel : CancelOrder
							cancel({ batchcode: this.source.batchcode }).then(res => {
								uni.hideLoading()
								if (res.code === 400000 || res.status === -1 || !res.status) {
									uni.showModal({
										content: res.message || res.msg,
										showCancel: false,
										success: cm => {
											if (cm.confirm) {
												if(!this.type) {
													this.source.status = -1
												} else {
													let act = this.source.tag === 6 ? 0 : 1
													uni.redirectTo({
														url: `/pages/orders/list?act=${act}`
													})													
												}
											}
										}
									})
								} else {
									uni.showModal({
										content: res.message,
										showCancel: false
									})									
								}
							})
						}
					}
			    })				
			}
		}
	}
</script>

<style lang="less">
	@import '../../../common/styles/variables.less';
	.control {
		display: flex;
		justify-content: flex-end;
		background-color: white;
		& > navigator, & > view {
			padding: 10upx 30upx;
			border-radius: 30upx;
			font-size: @minSize;
			margin: 15upx;
		}
		& > navigator {
			border:1px solid #eaeaea;
			&:not(:last-of-type) {
				margin-right: 15upx;
			}
			&.topay, &.special {
				border-color: @red;
				color: @red;
				&.act {
					background-color:@red;
					color: white;
				}
			}
		}
		& > view, & > navigator {
			border: 1px solid #f1f1f1;
			&:not(:last-of-type) {
				margin-right: 15upx;
			}
			&.topay, &.special {
				background-color: @red;
				color: white;
			}
		}
		.box {
			border-color: @red;
			color: @red;
		}
	}	
</style>
